package com.libraries.helper;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;

import com.libraries.base.BaseApplication;
import com.libraries.view.toast.Toaster;
import com.example.libraries.R;

public class FileHelper {
	private static final String TAG = "FileHelper";
	public static final int SIZETYPE_B = 1;// 获取文件大小单位为B的double值
	public static final int SIZETYPE_KB = 2;// 获取文件大小单位为KB的double值
	public static final int SIZETYPE_MB = 3;// 获取文件大小单位为MB的double值
	public static final int SIZETYPE_GB = 4;// 获取文件大小单位为GB的double值

	public static final String IMAGE_SMALL_CACHE_DIR = "images_small_cache";// 小图所放路径的文件夹名
	public static final String IMAGE_CACHE_DIR = "images_cache";// 默认图所放路径的文件夹名
	public static final String AUDIO_CACHE_DIR = "audios_cache";// 默认语音所放路径的文件夹名
	public static final String VIDEO_CACHE_DIR = "videos_cache";// 默认视频所放路径的文件夹名
	
	public static final int DO_CANCEL_RESULT = 5; // 取消
	public static final int DO_CAMERA_RESULT = 6;// 拍照
	public static final int DO_GALLERY_RESULT = 7; // 相册
	public static final int DO_CROP_RESULT = 8;// 裁剪结果

	/**
	 * 
	 * Object对象转byte数组
	 * 
	 * @param obj
	 * @return byte[]
	 */
	public static byte[] obj2Bytes(Object obj) {
		byte[] bytes = null;
		ByteArrayOutputStream bo = null;
		ObjectOutputStream oo = null;
		try {
			bo = new ByteArrayOutputStream();
			oo = new ObjectOutputStream(bo);
			oo.writeObject(obj);
			bytes = bo.toByteArray();
		} catch (Exception e) {
			Logger.log(TAG, "obj2Byte err", e);
		} finally {
			closeOutStreamQuietly(bo);
			closeOutStreamQuietly(oo);
		}
		return bytes;
	}

	/**
	 * 
	 * byte数组转对象
	 * 
	 * @param bytes
	 * @return Object
	 */

	public static Object bytes2Obj(byte[] bytes) {
		Object obj = null;
		ByteArrayInputStream bi = null;
		ObjectInputStream oi = null;
		try {
			bi = new ByteArrayInputStream(bytes);
			oi = new ObjectInputStream(bi);
			obj = oi.readObject();
		} catch (Exception e) {
			Logger.log(TAG, "bytes2Obj err", e);
		} finally {
			closeInStreamQuietly(bi);
			closeInStreamQuietly(oi);
		}
		return obj;
	}

	/**
	 * 
	 * 输入流关闭
	 * 
	 * @param inStream
	 *            void
	 */

	public static void closeInStreamQuietly(InputStream inStream) {
		if (inStream != null) {
			try {
				inStream.close();
			} catch (IOException e) {
				Logger.log(TAG, "closeInStreamQuietly err", e);
			} finally {
				inStream = null;
			}
		}

	}

	/**
	 * 
	 * 输出流关闭
	 * 
	 * @param outStream
	 *            void
	 */

	public static void closeOutStreamQuietly(OutputStream outStream) {
		if (outStream != null) {
			try {
				outStream.flush();
				outStream.close();
			} catch (IOException e) {
				Logger.log(TAG, "closeOutStreamQuietly err", e);
			} finally {
				outStream = null;
			}
		}
	}

	/**
	 * 获得指定文件的byte数组
	 */
	public static byte[] file2Bytes(File file) {
		if (!file.exists()) {
			return null;
		}
		FileInputStream fiStream = null;
		ByteArrayOutputStream boStream = null;
		try {
			fiStream = new FileInputStream(file);
			boStream = new ByteArrayOutputStream();
			byte[] bytes = new byte[1024 * 10];
			int len;
			while ((len = fiStream.read(bytes)) != -1) {
				boStream.write(bytes, 0, len);
			}
			return boStream.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (fiStream != null) {
					fiStream.close();
				}
				if (boStream != null) {
					boStream.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	/**
	 * 根据byte数组，生成文件
	 * 
	 * @param fileBytes
	 * @param fileInfo
	 */
	public static File bytes2File(byte[] fileBytes, String filePath) {
		File byteFile = new File(filePath);
		BufferedOutputStream bos = null;
		FileOutputStream fos = null;
		try {
			fos = new FileOutputStream(byteFile);
			bos = new BufferedOutputStream(fos);
			bos.write(fileBytes);
			return byteFile;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Logger.log(TAG, "bytes2File finally");
			if (bos != null) {
				try {
					bos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			if (fos != null) {
				try {
					fos.close();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
		}
		return null;
	}
	
	private static String getRootPath(){
		return Environment.getExternalStorageDirectory().getAbsolutePath();
	}

	/**
	 * 获取sdcard程序缓存路径
	 * 
	 * @return
	 */
	public static String getDiskPath(){
		File file = new File(getRootPath() + File.separator
				+ BaseApplication.application.getString(R.string.app_name_en));
		if (!file.exists()) {
			file.mkdirs();
		}
		return file.getAbsolutePath();
	}

	/**
	 * 获取内存中程序缓存路径
	 * 
	 * @return
	 */
	public static String getCachePath() {
		File file = new File(BaseApplication.application.getCacheDir().getAbsolutePath()
				+ File.separator + "cache");
		if (!file.exists()) {
			file.mkdirs();
		}
		return file.getAbsolutePath();
	}

	/**
	 * 获取sdcard中程序缓存路径
	 * 
	 * @return
	 */
	public static String getDiskCachePath(String cacheName) {
		File file = new File(getDiskPath() + File.separator + "cache"
				+ File.separator + cacheName);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file.getAbsolutePath();
	}

	/**
	 * 内存中程序数据存储路径
	 * 
	 * @param fileName
	 * @return
	 */
	public static File getDataFile(String fileName) {
		File file = new File("/data"
				+ Environment.getDataDirectory().getAbsolutePath()
				+ File.separator
				+ BaseApplication.application.getPackageName()
				+ File.separator + fileName);
		return file;
	}

	/**
	 * 自己拼接后缀名
	 * 
	 * @param format文件后缀名
	 * @return
	 */
	public static String getFileName(String format) {
		String fileName = "TMP_" + DateHelper.getDateFormat("yyyyMMddHHmmss")
				+ format;
		return fileName;
	}

	/**
	 * 创建文件
	 * 
	 * @param file
	 * @return
	 */
	public static File createNewFile(String filePath) {
		File file = new File(filePath);
		if (!file.exists()) {
			if (file.isDirectory()) {
				file.mkdirs();
			} else {
				try {
					file.createNewFile();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		return file;
	}

	/**
	 * 给定的文件路径和文件名 创建文件
	 * 
	 * @param filePath
	 *            给定的路径
	 * @param fileName
	 *            文件名
	 * @return
	 */
	public static File createNewFile(String filePath, String fileName) {
		File fileDir = new File(filePath);
		if (!fileDir.exists()) {
			fileDir.mkdir();
		}
		File file = new File(fileDir.getAbsolutePath() + "/" + fileName);
		if (file.exists()) {
			return createNewFile(filePath, fileName);
		} else {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return file;
		}
	}

	/**
	 * 从指定文件中读取对象
	 * 
	 * @param file
	 * @return object
	 */
	public static Object readObj(File file) {
		try {
			ObjectInputStream read = new ObjectInputStream(
					new BufferedInputStream(new FileInputStream(file)));
			Object obj = read.readObject();
			read.close();
			return obj;
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 通过uri获取文件路径
	 * 
	 * @param context
	 * @param fileUri
	 * @return
	 */
	@SuppressWarnings("deprecation")
	public static File getFileByUri(Activity context, Uri fileUri) {
		String[] proj = { MediaStore.Images.Media.DATA };
		Cursor cursor = context.managedQuery(fileUri, proj, null, null, null);
		if (cursor != null) {
			int column_index = cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
			cursor.moveToFirst();
			String img_path = cursor.getString(column_index);
			// 4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
			if (Integer.parseInt(Build.VERSION.SDK) < 14) {
				cursor.close();
			}
			File file = new File(img_path);
			return file;
		} else
			return null;
	}

	/**
	 * 通过文件路径获取Uri
	 * 
	 * @param filePath
	 * @return
	 */
	public static Uri getUriByFile(String filePath) {
		File file = new File(filePath);
		if (!file.exists()) {
			return null;
		}
		return Uri.fromFile(file);
	}

	
	public static String getFormat(String fileName) {
		int index = fileName.lastIndexOf(".");
		if (index != -1) {
			String format = fileName.substring(index, fileName.length());
			return format;
		}
		return ".png";
	}

	/**
	 * 获取文件路径
	 * 
	 * @param imgLink
	 * @return
	 */
	public static File getFileByLink(String imgLink) {
		if (!StringHelper.hasValue(imgLink)) {
			return null;
		}
		String format=getFormat(imgLink);
		File file = new File(getDiskCachePath(IMAGE_CACHE_DIR) 
				+ File.separator+ imgLink.hashCode() + format);
		return file;
	}
	
	
	/**
	 * read file
	 * 
	 * @param filePath
	 * @param charsetName
	 * @return if file not exist, return null, else return content of file
	 * @throws RuntimeException
	 *             if an error occurs while operator BufferedReader
	 */
	public static StringBuilder readFile(String filePath, String charsetName) {
		File file = new File(filePath);
		StringBuilder fileContent = new StringBuilder("");
		if (file == null || !file.isFile()) {
			return null;
		}

		BufferedReader reader = null;
		try {
			InputStreamReader is = new InputStreamReader(new FileInputStream(
					file), charsetName);
			reader = new BufferedReader(is);
			String line = null;
			while ((line = reader.readLine()) != null) {
				if (!fileContent.toString().equals("")) {
					fileContent.append("\r\n");
				}
				fileContent.append(line);
			}
			reader.close();
			return fileContent;
		} catch (IOException e) {
			throw new RuntimeException("IOException occurred. ", e);
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e) {
					throw new RuntimeException("IOException occurred. ", e);
				}
			}
		}
	}

	/**
	 * write file
	 * 
	 * @param filePath
	 * @param content
	 * @param append
	 *            is append, if true, write to the end of file, else clear
	 *            content of file and write into it
	 * @return return false if content is empty, true otherwise
	 * @throws RuntimeException
	 *             if an error occurs while operator FileWriter
	 */
	public static boolean writeFile(String filePath, String content,
			boolean append) {
		if (!StringHelper.hasValue(content)) {
			return false;
		}

		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(filePath, append);
			fileWriter.write(content);
			fileWriter.close();
			return true;
		} catch (IOException e) {
			throw new RuntimeException("IOException occurred. ", e);
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					throw new RuntimeException("IOException occurred. ", e);
				}
			}
		}
	}

	/**
	 * write file
	 * 
	 * @param filePath
	 * @param contentList
	 * @param append
	 */
	public static boolean writeFile(String filePath, List<String> contentList,
			boolean append) {

		FileWriter fileWriter = null;
		try {
			fileWriter = new FileWriter(filePath, append);
			int i = 0;
			for (String line : contentList) {
				if (i++ > 0) {
					fileWriter.write("\r\n");
				}
				fileWriter.write(line);
			}
			fileWriter.close();
			return true;
		} catch (IOException e) {
			throw new RuntimeException("IOException occurred. ", e);
		} finally {
			if (fileWriter != null) {
				try {
					fileWriter.close();
				} catch (IOException e) {
					throw new RuntimeException("IOException occurred. ", e);
				}
			}
		}
	}

	/**
	 * write file, the string will be written to the begin of the file
	 * 
	 * @param filePath
	 * @param content
	 * @return
	 */
	public static boolean writeFile(String filePath, String content) {
		return writeFile(filePath, content, false);
	}

	/**
	 * write file, the string list will be written to the begin of the file
	 * 
	 * @param filePath
	 * @param contentList
	 * @return
	 */
	public static boolean writeFile(String filePath, List<String> contentList) {
		return writeFile(filePath, contentList, false);
	}

	/**
	 * write file, the bytes will be written to the begin of the file
	 * 
	 * @param filePath
	 * @param stream
	 * @return
	 */
	public static boolean writeFile(String filePath, InputStream stream) {
		return writeFile(filePath, stream, false);
	}

	/**
	 * write file
	 * 
	 * @param file
	 *            the file to be opened for writing.
	 * @param stream
	 *            the input stream
	 * @param append
	 *            if <code>true</code>, then bytes will be written to the end of
	 *            the file rather than the beginning
	 * @return return true
	 * @throws RuntimeException
	 *             if an error occurs while operator FileOutputStream
	 */
	public static boolean writeFile(String filePath, InputStream stream,
			boolean append) {
		return writeFile(filePath != null ? new File(filePath) : null, stream,
				append);
	}

	/**
	 * write file, the bytes will be written to the begin of the file
	 * 
	 * @param file
	 * @param stream
	 * @return
	 */
	public static boolean writeFile(File file, InputStream stream) {
		return writeFile(file, stream, false);
	}

	/**
	 * write file
	 * 
	 * @param file
	 *            the file to be opened for writing.
	 * @param stream
	 *            the input stream
	 * @param append
	 *            if <code>true</code>, then bytes will be written to the end of
	 *            the file rather than the beginning
	 * @return return true
	 * @throws RuntimeException
	 *             if an error occurs while operator FileOutputStream
	 */
	public static boolean writeFile(File file, InputStream stream,
			boolean append) {
		OutputStream o = null;
		try {
			o = new FileOutputStream(file, append);
			byte data[] = new byte[1024];
			int length = -1;
			while ((length = stream.read(data)) != -1) {
				o.write(data, 0, length);
			}
			o.flush();
			return true;
		} catch (FileNotFoundException e) {
			throw new RuntimeException("FileNotFoundException occurred. ", e);
		} catch (IOException e) {
			throw new RuntimeException("IOException occurred. ", e);
		} finally {
			if (o != null) {
				try {
					o.close();
					stream.close();
				} catch (IOException e) {
					throw new RuntimeException("IOException occurred. ", e);
				}
			}
		}
	}

	/**
	 * copy file
	 * @param sourceFilePath
	 * @param destFilePath
	 * @return
	 * @throws RuntimeException
	 *             if an error occurs while operator FileOutputStream
	 */
	public static boolean copyFile(String sourceFilePath, String destFilePath) {
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(sourceFilePath);
		} catch (FileNotFoundException e) {
			throw new RuntimeException("FileNotFoundException occurred. ", e);
		}
		return writeFile(destFilePath, inputStream);
	}


	/**
	 * delete file or directory
	 * @param path
	 * @return
	 */
	public static boolean deleteFile(String path) {
		if (!StringHelper.hasValue(path)) {
			return true;
		}

		File file = new File(path);
		if (!file.exists()) {
			return true;
		}
		if (file.isFile()) {
			return file.delete();
		}
		if (!file.isDirectory()) {
			return false;
		}
		for (File f : file.listFiles()) {
			if (f.isFile()) {
				f.delete();
			} else if (f.isDirectory()) {
				deleteFile(f.getAbsolutePath());
			}
		}
		return file.delete();
	}

	/**
	 * 转换文件大小
	 * 
	 * @param fileS
	 * @return
	 */
	public static String getFileSize(long fileSize) {
		DecimalFormat df = new DecimalFormat("#.00");
		String fileSizeString = "";
		String wrongSize = "0B";
		if (fileSize == 0) {
			return wrongSize;
		}
		if (fileSize < 1024) {
			fileSizeString = df.format((double) fileSize) + "B";
		} else if (fileSize < 1048576) {
			fileSizeString = df.format((double) fileSize / 1024) + "KB";
		} else if (fileSize < 1073741824) {
			fileSizeString = df.format((double) fileSize / 1048576) + "MB";
		} else {
			fileSizeString = df.format((double) fileSize / 1073741824) + "GB";
		}
		return fileSizeString;
	}

	public static boolean isPictureFile(String fileName){
		if (fileName==null) {
			return false;
		}
		if (fileName.endsWith(".jpg")
				||fileName.endsWith(".gif")
				||fileName.endsWith(".bmp")
				||fileName.endsWith(".png")
				||fileName.endsWith(".jpeg")
				) {
			return true;
		}
		return false;
	}
	

	/**
	 * 利用ContentProvider扫描手机中的图片，此方法在运行在子线程中
	 * 
	 * @param context
	 * @param refreshHandler
	 */
	public static void loadLocalVideos(final Context context,
			final Handler refreshHandler) {
		if (!DeviceHelper.isMountting()) {
			Toaster.show(context, R.string.unload_device);
			refreshHandler.sendEmptyMessage(-1);
			return;
		}
		DeviceHelper.doMediaScanner(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				ArrayList<String> lVideos = new ArrayList<String>();
				String[] projection = new String[] { MediaStore.Video.Media.DATA};// 图片绝对路径

				Uri videoUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
				Cursor mCursor = context.getContentResolver().query(videoUri,
						projection, null,null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
				while (mCursor.moveToNext()) {
					String filePath = mCursor.getString(mCursor
							.getColumnIndex(MediaStore.Video.Media.DATA));
					// Logger.log(this, "loadLocalImages while",filePath);
					lVideos.add(filePath);
				}
				Message message = Message.obtain(refreshHandler);
				message.obj = lVideos;
				refreshHandler.sendMessage(message);
				mCursor.close();
			}
		}).start();
	}
	
	/**
	 * 利用ContentProvider扫描手机中的图片，此方法在运行在子线程中
	 * 
	 * @param context
	 * @param refreshHandler
	 */
	public static void loadLocalAudios(final Context context,
			final Handler refreshHandler) {
		if (!DeviceHelper.isMountting()) {
			Toaster.show(context, R.string.unload_device);
			refreshHandler.sendEmptyMessage(-1);
			return;
		}
		DeviceHelper.doMediaScanner(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				ArrayList<String> lAudios = new ArrayList<String>();
				String[] projection = new String[] { MediaStore.Audio.Media.DATA};// 图片绝对路径

				Uri audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
				Cursor mCursor = context.getContentResolver().query(audioUri,
						projection, null, null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
				while (mCursor.moveToNext()) {
					String filePath = mCursor.getString(mCursor
							.getColumnIndex(MediaStore.Audio.Media.DATA));
					lAudios.add(filePath);
				}
				Message message = Message.obtain(refreshHandler);
				message.obj = lAudios;
				refreshHandler.sendMessage(message);
				mCursor.close();
			}
		}).start();
	}

	/**
	 * 利用ContentProvider扫描手机中的图片，此方法在运行在子线程中
	 * 
	 * @param context
	 * @param refreshHandler
	 */
	public static void loadLocalImages(final Context context,
			final Handler refreshHandler) {
		if (!DeviceHelper.isMountting()) {
			Toaster.show(context, R.string.unload_device);
			refreshHandler.sendEmptyMessage(-1);
			return;
		}
		DeviceHelper.doMediaScanner(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				ArrayList<String> lImages = new ArrayList<String>();
				String[] projection = new String[] { MediaStore.Images.Media.DATA };// 图片绝对路径
				// 条件
				String selection = MediaStore.Images.Media.MIME_TYPE + "=? or "
						+ MediaStore.Images.Media.MIME_TYPE + "=? or "
						+ MediaStore.Images.Media.MIME_TYPE + "=? or "
						+ MediaStore.Images.Media.MIME_TYPE + "=?";
				String[] selectionArgs = { "image/jpeg", "image/png",
						"image/bmp", "image/gif" };
				String sortOrder = MediaStore.Images.Media.DATE_MODIFIED
						+ " desc";

				Uri imgUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
				Cursor mCursor = context.getContentResolver().query(imgUri,
						projection, selection, selectionArgs, sortOrder);
				while (mCursor.moveToNext()) {
					String filePath = mCursor.getString(mCursor
							.getColumnIndex(MediaStore.Images.Media.DATA));
					// Logger.log(this, "loadLocalImages while",filePath);
					lImages.add(filePath);
				}
				Message message = Message.obtain(refreshHandler);
				message.obj = lImages;
				refreshHandler.sendMessage(message);
				mCursor.close();
			}
		}).start();
	}
	
	/**
	 * 利用ContentProvider扫描手机中的图片，此方法在运行在子线程中
	 * 
	 * @param context
	 * @param refreshHandler
	 */
	public static void loadLocalDocuments(final Context context,
			final Handler refreshHandler) {
		if (!DeviceHelper.isMountting()) {
			Toaster.show(context, R.string.unload_device);
			refreshHandler.sendEmptyMessage(-1);
			return;
		}
		DeviceHelper.doMediaScanner(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				ArrayList<String> lImages = new ArrayList<String>();
				File rootFile=new File(FileHelper.getRootPath());
				listDocumentFiles(rootFile, lImages);
				Message message = Message.obtain(refreshHandler);
				message.obj = lImages;
				refreshHandler.sendMessage(message);
			}
		}).start();
	}

	private static void listDocumentFiles(File rootFile,ArrayList<String> lImages){
		for (File file : rootFile.listFiles()) {
			if (file.isDirectory()) {
				listDocumentFiles(file,lImages);
			}
			else {
				String filePath=file.getAbsolutePath();
				if (filePath.endsWith(".doc")
						||filePath.endsWith(".docx")
						||filePath.endsWith(".xlsx")
						||filePath.endsWith(".pdf")
						||filePath.endsWith(".xls")
						||filePath.endsWith(".ppt")
						) {
					lImages.add(filePath);
				}
			}
		}
	}
	
	/**
	 * 利用ContentProvider扫描手机中的图片，此方法在运行在子线程中
	 * 
	 * @param context
	 * @param refreshHandler
	 */
	public static void loadLocalApplications(final Context context,
			final Handler refreshHandler) {
		if (!DeviceHelper.isMountting()) {
			Toaster.show(context, R.string.unload_device);
			refreshHandler.sendEmptyMessage(-1);
			return;
		}
		DeviceHelper.doMediaScanner(context);
		new Thread(new Runnable() {
			@Override
			public void run() {
				ArrayList<String> lApplications = new ArrayList<String>();
				File rootFile=new File(FileHelper.getRootPath());
				listApplicationFiles(rootFile, lApplications);
				Message message = Message.obtain(refreshHandler);
				message.obj = lApplications;
				refreshHandler.sendMessage(message);
			}
		}).start();
	}
	
	private static void listApplicationFiles(File rootFile,ArrayList<String> lApplications){
		for (File file : rootFile.listFiles()) {
			if (file.isDirectory()) {
				listApplicationFiles(file,lApplications);
			}
			else {
				String filePath=file.getAbsolutePath();
				if (filePath.endsWith(".apk")
						||filePath.endsWith(".ipa")
						) {
					lApplications.add(filePath);
				}
			}
		}
	}
	
//	/**
//	 * 生成二维码
//	 * 
//	 * @param iv_qrocde
//	 * @param qrcodeString
//	 * @param width
//	 * @param height
//	 */
//	public static void createQrcodeImage(ImageView iv_qrocde,
//			String qrcodeString, int width, int height) {
//		try {
//			// 判断URL合法性
//			if (!StringHelper.hasValue(qrcodeString)) {
//				return;
//			}
//			Hashtable<EncodeHintType, String> hints = new Hashtable<EncodeHintType, String>();
//			hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//			// 图像数据转换，使用了矩阵转换
//			BitMatrix bitMatrix = new QRCodeWriter().encode(qrcodeString,
//					BarcodeFormat.QR_CODE, width, height, hints);
//			int[] pixels = new int[width * height];
//			// 下面这里按照二维码的算法，逐个生成二维码的图片，
//			// 两个for循环是图片横列扫描的结果
//			for (int y = 0; y < height; y++) {
//				for (int x = 0; x < width; x++) {
//					if (bitMatrix.get(x, y)) {
//						pixels[y * width + x] = 0xff000000;
//					} else {
//						pixels[y * width + x] = 0xffffffff;
//					}
//				}
//			}
//			// 生成二维码图片的格式，使用ARGB_8888
//			Bitmap bitmap = Bitmap.createBitmap(width, height,
//					Bitmap.Config.ARGB_8888);
//			bitmap.setPixels(pixels, 0, width, 0, 0, width, height);
//			// 显示到一个ImageView上面
//			iv_qrocde.setImageBitmap(bitmap);
//		} catch (WriterException e) {
//			e.printStackTrace();
//		}
//	}
}
